<?php
namespace app\admin\controller;

class Membercoupons extends Admin{
    public function __construct(){
        parent::__construct();
        $this->model = "MemberCoupons";
        $this->assign("model",$this->model);
    }

    public function get_where_map(){
        $status = input('param.status/d');
        $couponsid = input('param.couponsid/d');
        $startdate = input('param.startdate/s');
        $enddate = input('param.enddate/s');
        $checkstartdate = input('param.checkstartdate/s'); // 核销开始时间
        $checkenddate = input('param.checkenddate/s'); // 核销结束时间
        $dealerid = input('param.dealerid/d');
        $source = input('param.source/d');
        $keywords = input('param.keywords/s');
        $whereDatas = [];
        if (!is_null($status) && $status != -1) {
            $whereDatas['status'] = $status;
        }
        if ($couponsid) {
            $whereDatas['couponsid'] = $couponsid;
        }
        if ($dealerid) {
            $whereDatas['dealerid'] = $dealerid;
        }else{
            if ($this->uinfo['roleid'] == 4) {
                $dealerids = model('AdminUser')->where('puserid', $this->uinfo['userid'])->where('roleid', 6)->column('userid');
                $whereDatas['dealerid'] = ['in', $dealerids];
            }elseif ($this->uinfo['roleid'] == 6) {
                $whereDatas['dealerid'] = $this->uinfo['userid'];
            }
        }
        if (!is_null($source) && $source != -1) {
            $whereDatas['source'] = $source;
        }
        $starttime = $startdate ? strtotime($startdate) : 0;
        $endtime = $enddate ? strtotime($enddate) : 0;
        if ($starttime && $endtime) {
            $whereDatas['addtime'] = ['between', [$starttime,$endtime]];
        }elseif ($starttime && !$endtime) {
            $whereDatas['addtime'] = ['egt', $starttime];
        }elseif ($endtime && !$starttime) {
            $whereDatas['addtime'] = ['elt', $endtime];
        }
        $checkstartdate = $checkstartdate ? strtotime($checkstartdate) : 0;
        $checkenddate = $checkenddate ? strtotime($checkenddate) : 0;
        if ($checkstartdate && $checkenddate) {
            $whereDatas['usetime'] = ['between', [$checkstartdate,$checkenddate]];
        }elseif ($checkstartdate && !$checkenddate) {
            $whereDatas['usetime'] = ['egt', $checkstartdate];
        }elseif ($checkenddate && !$checkstartdate) {
            $whereDatas['usetime'] = ['elt', $checkenddate];
        }
        if ($keywords) {
            $where['realname'] = ['like',  '%' . $keywords . '%'];
			$userid = model('AdminUser')->where($where)->column('userid');
			if ($userid) {
				$whereDatas['userid'] = ['in', $userid];
			} else {
                //$whereDatas['userid'] = '';
				$whereDatas['couponsno'] = ['like', "%$keywords%"];
            }
        }
        return $whereDatas;
    }
    public function index()
    {
        $where['status'] = 0;
        if ($this->uinfo['roleid'] == 1) {
            $where['roleid'] = 6;
        }elseif ($this->uinfo['roleid'] == 4) {
            $where['roleid'] = 6;
            $where['puserid'] = $this->uinfo['userid'];
        }elseif ($this->uinfo['roleid'] == 6) {
            $where['userid'] = $this->uinfo['userid'];
        }
        $deallist = model('AdminUser')->where($where)->order('userid desc')->column('userid, username');
        $couponsname = model('Coupons')->column('id, name');
        $this->assign('deallist', $deallist);
        $this->assign('couponsname', $couponsname);
        return $this->fetch();
    }
    public function audit(){
    	$ids = input('ids/s');
        $info = model($this->model)->where('id', $ids)->find();
        if ($info['status'] != 0) {
            return json(['status'=>0,'msg'=>'请不要重复操作']);
        }
    	$isok = model($this->model)->where('id', $ids)->update(['status'=>1,'adminid'=>$this->uinfo['userid'],'sendtime'=>time()]);
    	if ($isok === false) {
    		return json(['status'=>0,'msg'=>'操作失败！']);
    	}
    	return json(['status'=>1,'msg'=>'发放成功！']);
    }
}